﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cua_Hang.DTO;
using System.Data;

namespace Cua_Hang.DAO
{
    public class ChiTiet_CuaHang_DAO
    {


        //==================================================================
        //-------load tat ca chi tiet cua hang tren menu trai-----------
        public static List<ChiTiet_CuaHang_DTO> ListChiTiet_CuaHangAll_DAO(int ID_cuahang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang AS T1 JOIN ChiTiet_CuaHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang WHERE T2.ID_cuahang ='" +ID_cuahang+"'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_CuaHang_DTO> lstChiTiet_CuaHang = new List<ChiTiet_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_CuaHang_DTO dm = new ChiTiet_CuaHang_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_ChiTiet_CuaHang = dr["Ten_ChiTiet_CuaHang"].ToString();

                    lstChiTiet_CuaHang.Add(dm);
                }
                return lstChiTiet_CuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //------- ket thuc tat ca chi tiet cua hang tren menu trai------------
        //==================================================================
        //==================================================================
        //-------load tat ca chi tiet cua hang -----------
        public static List<ChiTiet_CuaHang_DTO> ListChiTiet_CuaHangAll_Admin_DAO()
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_CuaHang";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_CuaHang_DTO> lstChiTiet_CuaHang = new List<ChiTiet_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_CuaHang_DTO dm = new ChiTiet_CuaHang_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_ChiTiet_CuaHang = dr["Ten_ChiTiet_CuaHang"].ToString();

                    lstChiTiet_CuaHang.Add(dm);
                }
                return lstChiTiet_CuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //------- ket thuc tat ca chi tiet cua hang ------------
        //==================================================================
        //==================================================================
        //-------load tat ca chi tiet cua hang theo id-----------
        public static List<ChiTiet_CuaHang_DTO> ListChiTiet_CuaHangTheoID_DAO(int ID_ChiTiet_CuaHang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM  ChiTiet_CuaHang  WHERE ID_ChiTiet_CuaHang like '%" + ID_ChiTiet_CuaHang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_CuaHang_DTO> lstChiTiet_CuaHang = new List<ChiTiet_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_CuaHang_DTO dm = new ChiTiet_CuaHang_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_ChiTiet_CuaHang = dr["Ten_ChiTiet_CuaHang"].ToString();

                    lstChiTiet_CuaHang.Add(dm);
                }
                return lstChiTiet_CuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //------- ket thuc tat ca chi tiet cua hang theo id------------
        //==================================================================
        //==================================================================
        //-----------them chi tiet cua hang ------------

        public static void ThemChiTietCuaHang_DAO(ChiTiet_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "INSERT INTO ChiTiet_CuaHang(ID_cuahang,Ten_ChiTiet_CuaHang) VALUES(";
                //strSQL += "N'" + kh.Ten_KhachHang + "',";
                //strSQL += "N'" + kh.DiaChi_KhachHang + "',";
                //strSQL += "N'" + kh.Email_KhachHang + "',";
                strSQL += "N'" + ch.ID_cuahang + "',";
                strSQL += "N'" + ch.Ten_ChiTiet_CuaHang + "')";
                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }


        //-------ket thuc them chi tiet cua hang  ------------
        //==============================================================
        //==============================================================
        //------ xoa chi tiet cua hang ------------
        public static void XoaChiTietCuaHang_DAO(ChiTiet_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "DELETE FROM ChiTiet_CuaHang WHERE ID_ChiTiet_CuaHang ='" + ch.ID_ChiTiet_CuaHang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc xoa chi tiet cua hang ------------

        //==============================================================
        //==============================================================
        //------ sua chi tiet cua hang  ------------
        public static void SuaChiTietCuaHang_DAO(ChiTiet_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "UPDATE ChiTiet_CuaHang SET Ten_ChiTiet_CuaHang = N'" + ch.Ten_ChiTiet_CuaHang + "' WHERE ID_ChiTiet_CuaHang = '" + ch.ID_ChiTiet_CuaHang + "'";
                //string strSQL = "UPDATE ChiTiet_CuaHang " +
                 //"SET Ten_cuahang = '" + ch.Ten_cuahang +

                //" DiaChi = '" + info.DiaChi + "', " +
                //" NgaySinh = '" + info.NgaySinh.ToShortDateString() + "', " +
                //" MaLop = '" + info.MaLop + "'," +
                //" GioiTinh = '" + info.GioiTinh + "', " +
                //" NamHoc = " + info.NamHoc +
                // " WHERE ID_cuahang = '" + ch.ID_cuahang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc sua chi tiet cua hang  ------------
        //==============================================================
        //==============================================================
        //------ ham kiem tra chi tiet cua hang co trong csdl hay chua ------------
        public static Boolean KiemTraTrung_ChiTiet_CuaHang_DAO(ChiTiet_CuaHang_DTO ch)
        {
            Boolean kq = false;
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_CuaHang  WHERE Ten_ChiTiet_CuaHang =N'" + ch.Ten_ChiTiet_CuaHang + "'";
                DataTable dt = d.ExecuteQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    kq = true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
            return kq;
        }
        //-------ket thuc ham kiem tra chi tiet cua hang co trong csdl hay chua -----------

        //==============================================================
        

       


    }
}